import store from '../store'
function formatNumber (n) {
  const str = n.toString()
  return str[1] ? str : `0${str}`
}

export function formatTime (date) {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  const t1 = [year, month, day].map(formatNumber).join('-')
  const t2 = [hour, minute, second].map(formatNumber).join(':')

  return `${t1} ${t2} `
}
export function formatComment (comment) {
  return {
    id: comment.M.Ci,
    author: comment.M.N,
    phone: comment.M.Ta,
    floor: comment.M.SF || `${comment.F}楼`,
    content: comment.M.C.replace(/<img/g, '<img width="100%"')
  }
}
export function predivImage (e) {
  wx.previewImage({
    current: e.currentTarget.id, // 当前显示图片的http链接
    urls: this.files // 需要预览的图片http链接列表
  })
}
export function formatNewsList (news) {
  const { newsid, title, postdate, commentcount, lapinid, image } = news
  return {
    id: newsid,
    title,
    postdate: formatTime(postdate),
    commentcount,
    lapinid,
    image,
    link: `/pages/news/detail?id=${newsid}&title=${title}`
  }
}
export function getToken2 () {
  return new Promise((resolve, reject) => {
    console.log('getToken2')
    wx.login({
      success: (res) => {
        var code = res.code
        if (res.code) {
          wx.request({
            url: 'https://xcx.taosha.co/api/wechat/login',
            data: {
              js_code: code
            },
            success: (res) => {
              console.log(res)
              wx.setStorageSync('token', res.data.token)
              wx.setStorageSync('timestamp', res.data.timestamp)
              wx.setStorageSync('uid', res.data.uid)
              resolve()
            },
            fail (res) {
              console.log(res)
              reject()
            }
          }
          )
        }
      }
    })
  })
}
export function  getCookie(c_name) {
  if (document.cookie.length > 0) {
   let c_start = document.cookie.indexOf(c_name + "=")
    if (c_start != -1) {
      c_start = c_start + c_name.length + 1
   let  c_end = document.cookie.indexOf(";", c_start)
      if (c_end == -1) c_end = document.cookie.length
      return unescape(document.cookie.substring(c_start, c_end))
    }
  }
}
export function getToken () {
  return new Promise((resolve, reject) => {
    wx.login({
      success (res) {
        const code = res.code
        if (res.code) {
          wx.request({
            url: 'https://xcx.taosha.co/api/wechat/login',
            data: {
              js_code: code
            },
            success (res) {
              wx.setStorageSync('token', res.data.token)
              wx.setStorageSync('timestamp', res.data.timestamp)
              wx.setStorageSync('uid', res.data.uid)
              store.dispatch('initParam', {
                uid: res.data.uid,
                timestamp: res.data.timestamp,
                token: res.data.token
              })
              resolve(res.data.token, res.data.timestamp, res.data.uid)
            },
            fail (e) {
              console.log('request错误', e)
              reject(e)
            }
          })
        }
      },
      fail (e) {
        console.log('login错误', e)
        reject(e)
      }
    })
  })
}

export function getUrl (url) {
  return new Promise((resolve, reject) => {
    wx.request({
      url: url,
      success: function (res) {
        if (res.data.code === 0) {
          resolve(res.data.data)
        } else {
          reject(res.data)
        }
      }
    })
  })
}
export function getUrl2 (url) {
  return new Promise((resolve, reject) => {
    wx.request({
      url: url,
      success: function (res) {
        if (res.data.code === 0) {
          resolve()
        } else {
          reject()
        }
      }
    })
  })
}
export function getQueryParam(name, url) {
  //URL GET 获取值
  var reg = new RegExp("(^|\\?|&)" + name + "=([^&]*)(\\s|&|$)", "i"),
    Url = url || location.href;
  if (reg.test(Url))
    return decodeURI(RegExp.$2.replace(/\+/g, " "));
  return "";
}
